package com.zzgc.modules.controller;

import com.zzgc.common.entity.*;
import com.zzgc.common.response.HttpStatus;
import com.zzgc.common.response.Result;
import com.zzgc.common.utils.StringUtils;
import com.zzgc.modules.log.ApiLog;
import com.zzgc.modules.param.Sort;
import com.zzgc.modules.param.StudentArchivesParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.Array;
import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * 学生档案控制类
 *
 * @Author:
 * @CreateTime: 2022-12-02  09:22
 * @Description:
 * @Version: 1.0
 */
@RestController
@RequestMapping("archive")
@ApiLog(value = "学生档案管理控制类", url = "archive")
@Api(tags = "学生档案管理控制类")
public class StudentArchivesController extends BaseController {


    @GetMapping("get")
    @ApiLog(value = "获取学生视力档案", url = "get", method = RequestMethod.GET)
    @ApiOperation(value = "获取学生视力档案")
    public Result getStudentArchives(StudentArchivesParam archivesParam,String district,String city) {
        String userRole = super.getUserRole();
        //判断是否是河南省教育局
        if (!userRole.equals("1") && !userRole.equals("2")) {
            String dist =super.getDepartId();
            AuthDepart depart = departService.queryById(Long.parseLong(dist));
            city = depart.getAIdBak();
        }

        List<String> tableName = studentTableDao.queryAllTableName();
        HashMap<String, Object> map = new HashMap<>();
        List<String> list = new ArrayList<>();
        // 判断是否选择市
        if (StringUtils.isNotEmpty(city)) {
            // 根据市名字查询代码
            SysDict sysDict = sysDictService.queryByName(city);
            city = sysDict.getSCode();
            for (String s : tableName) {
                if (!s.contains(city)){ // 排除不是这个市的表
                    list.add(s);
                }
            }
            tableName.removeAll(list);
            archivesParam.setTableNames(tableName);
        }
        // 判断是否选择年份
        if (StringUtils.isNotEmpty(archivesParam.getSYear())) {
            list = new ArrayList<>();
            for (String name : tableName) {
                if (!name.contains(archivesParam.getSYear())) { // 排除不是这个年份的数据
                    list.add(name);
                }
            }
            tableName.removeAll(list);
            archivesParam.setTableNames(tableName);
        }
        if (StringUtils.isEmpty(tableName)){ // 没有表数据直接返回
            map.put("total", 0);
            map.put("data", new ArrayList<>());
            return Result.builder().code(HttpStatus.OK.value()).msg("暂无数据").data(map).build();
        }
        // 根据区县名字查询代码
        if (StringUtils.isNotEmpty(district)) {
            SysDictItem sysDictItem = sysDictItemService.queryByName(city);
            archivesParam.setTableName(sysDictItem.getSCode()); // 把区县代码传到sql语句上
        }

        archivesParam.setTableNames(tableName);
        archivesParam.setSorts(Arrays.asList(new Sort("a.s_year","DESC"),new Sort("a.s_number")));
        // 三个字段合成一个字段用 ，姓名，手机号，身份证号。用name字段代替
        archivesParam.setSMobile(archivesParam.getSName());
        archivesParam.setSIdCard(archivesParam.getSName());
        List<StudentArchivesParam> archivesParams = studentArchivesService.queryAllByArchives(archivesParam);

        map.put("total", archivesParams.size() == 0 ? 0 : archivesParams.get(0).getTotal());
        map.put("data", archivesParams);
        return Result.builder().code(HttpStatus.OK.value())
                .msg(HttpStatus.OK.getReasonPhrase()).data(map).build();
    }

    @GetMapping("get/plan")
    @ApiLog(value = "获取筛查计划", url = "get/plan", method = RequestMethod.GET)
    @ApiOperation(value = "获取筛查计划")
    public Result getSchoolPlan( String district, String schoolName, Integer size, Integer page,String dist,String city) {
        String userRole = super.getUserRole();
        //判断是否是河南省教育局
        if (!userRole.equals("1") && !userRole.equals("2")) {
            dist =super.getDepartId();
            AuthDepart depart = departService.queryById(Long.parseLong(dist));
            city = depart.getAIdBak();
        }
        SchoolPlan schoolPlan = new SchoolPlan();
        HashMap<Object, Object> map = new HashMap<>();

        if (StringUtils.isNotEmpty(district)) {
            SysDictItem sysDictItem = sysDictItemService.queryByName(district);
            schoolPlan.setSAreaCode(sysDictItem.getSCode());
        }
        if (StringUtils.isNotEmpty(city)){
            SysDict sysDict = sysDictService.queryByName(city);
            schoolPlan.setSParentCode(sysDict.getSCode());
        }
        if (StringUtils.isNotEmpty(schoolName)) {
            School school = schoolService.queryByName(schoolName);
            if (StringUtils.isNull(school)) {
                map.put("total", 0);
                map.put("data", new ArrayList<>());
                return Result.builder().code(HttpStatus.OK.value())
                        .msg(HttpStatus.OK.getReasonPhrase()).data(map).build();
            }
            schoolPlan.setSSchoolId(school.getId());
        }
        if (StringUtils.isNull(size)) {
            size = 10;
        }
        if (StringUtils.isNull(page)) {
            page = 1;
        }
        schoolPlan.setSize(size);
        schoolPlan.setPage(page);
        List<SchoolPlan> schoolPlans = schoolPlanService.queryByPage(schoolPlan);

        //schoolPlans.subList();
        map.put("total", schoolPlans.size() == 0 ? 0 : schoolPlans.get(0).getTotal());
        map.put("data", schoolPlans);
        return Result.builder().code(HttpStatus.OK.value())
                .msg(HttpStatus.OK.getReasonPhrase()).data(map).build();
    }
}
